package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.PlantingImage;
import com.fsnbigdata.domain.Planting;
import com.fsnbigdata.domain.QPlantingImage;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = PlantingImageRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = PlantingImageRepositoryCustom.class)
@Transactional(readOnly = true)
public class PlantingImageRepositoryImpl extends QueryDslRepositorySupportExt<PlantingImage> implements PlantingImageRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    PlantingImageRepositoryImpl() {
        super(PlantingImage.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LOCATION = "location";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PLANTING = "planting";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CODE = "code";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATE_DATE = "createDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String AUTHOR = "author";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String URL = "url";

    /**
     * TODO Auto-generated method documentation
     *
     * @param planting
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<PlantingImage> findByPlanting(Planting planting, GlobalSearch globalSearch, Pageable pageable) {
        QPlantingImage plantingImage = QPlantingImage.plantingImage;
        JPQLQuery<PlantingImage> query = from(plantingImage);
        Assert.notNull(planting, "planting is required");
        query.where(plantingImage.planting.eq(planting));
        Path<?>[] paths = new Path<?>[] { plantingImage.planting, plantingImage.code, plantingImage.description, plantingImage.location, plantingImage.author, plantingImage.url, plantingImage.createDate };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(PLANTING, plantingImage.planting).map(CODE, plantingImage.code).map(DESCRIPTION, plantingImage.description).map(LOCATION, plantingImage.location).map(AUTHOR, plantingImage.author).map(URL, plantingImage.url).map(CREATE_DATE, plantingImage.createDate);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, plantingImage);
    }
}
